package com.mflex.mwms.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.TeslaCompareAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.Task;
import com.mflex.mwms.model.TaskDetail;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CcsCompareActivity extends BaseActivity {
    @BindView(R.id.stv_DeliverGoodSno)
    EditText stvDeliverGoodSno;

    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;

    @BindView(R.id.lvTaskList)
    ListView lvTaskList;

    MiniLoadingDialog mMiniLoadingDialog;

    List<Task> taskList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ccs_compare);
        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        taskList = new ArrayList<>();
        TeslaCompareAdapter teslaCompareAdapter = new TeslaCompareAdapter(this, taskList);
        lvTaskList.setAdapter(teslaCompareAdapter);

        stvDeliverGoodSno.setOnKeyListener(this);
        stvBoxNumber.setOnKeyListener(this);

        stvDeliverGoodSno.requestFocus();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_DeliverGoodSno:
                    requestTask(view);
                    break;
                case R.id.stv_BoxNumber:
                    requestTaskBox(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void requestTask(View view) {
        if (StringUtils.isEmpty(stvDeliverGoodSno.getText().toString())) {
            return;
        }
        mMiniLoadingDialog.show();
        XHttp.get("api/Task/GetShipmentTaskByDeliverNo")
                .params("DeliverGoodSno", stvDeliverGoodSno.getText().toString().split("&")[0])
                .params("WareHouseName", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<List<Task>>, List<Task>>(new SimpleCallBack<List<Task>>() {

                    @Override
                    public void onSuccess(List<Task> response) {
                        mMiniLoadingDialog.dismiss();
                        taskList = response;
                        TeslaCompareAdapter teslaCompareAdapter = new TeslaCompareAdapter(CcsCompareActivity.this, taskList);
                        lvTaskList.setAdapter(teslaCompareAdapter);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvDeliverGoodSno.setText("");
                        stvDeliverGoodSno.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                stvDeliverGoodSno.requestFocus();
                            }
                        },200);
                        MessageUtils.errorMsgAndVoice(CcsCompareActivity.this, e);
                    }
                }) {
                });
    }

    private void requestTaskBox(View view) {
        if (StringUtils.isEmpty(stvBoxNumber.getText().toString())) {
            return;
        }
        mMiniLoadingDialog.show();
        Task taskSelected = null;
        for (Task task : taskList) {
            for (TaskDetail taskDetail : task.taskDetails) {
                if (StringUtils.equals(stvBoxNumber.getText().toString(), taskDetail.getDetailName())) {
                    taskSelected = task;
                    break;
                }
            }
        }
        mMiniLoadingDialog.dismiss();
        if (taskSelected == null) {
            MessageUtils.errorMsgAndVoice(CcsCompareActivity.this, "该箱号不在此送货单中");
        } else {
            Intent intent = new Intent(CcsCompareActivity.this, CcsCompareDetailActivity.class);
            intent.putExtra("task", JsonUtil.toJson(taskSelected));
            startActivityForResult(intent, 0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        stvBoxNumber.setText("");
        requestTask(CcsCompareActivity.this.getCurrentFocus());
    }
}